package com.ruoyi.app.controller;

import java.util.List;
import java.util.Map;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.app.domain.bo.AppUserBo;
import com.ruoyi.app.domain.vo.AppUserVo;
import com.ruoyi.app.service.AppLoginService;
import com.ruoyi.app.service.AppUserService;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.bo.LoginAppBody;
import com.ruoyi.common.core.domain.bo.LoginAppUser;
import com.ruoyi.common.satoken.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.idempotent.annotation.RepeatSubmit;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.web.core.BaseController;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

/**
 * 用户管理
 *
 * @author zcc
 * @date 2024-12-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/user")
public class AppUserController extends BaseController {

    private final AppUserService appUserService;
    private final AppLoginService appLoginService ;



    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @SaIgnore
    @PostMapping("/login")
    public R<Map<String, Object>> login(@RequestBody LoginAppBody loginBody) {
        // 生成令牌
        String token = appLoginService.wxLogin(loginBody.getCode());
        if(token==null){
            return R.warn("授权失败");
        }
        return R.ok(Map.of(Constants.TOKEN, token));
    }
    /**
     * 查询用户管理列表
     */
    @SaCheckPermission("app:user:list")
    @GetMapping("/list")
    public TableDataInfo<AppUserVo> list(AppUserBo bo, PageQuery pageQuery) {
        return appUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户管理列表
     */
    @SaCheckPermission("app:user:export")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppUserBo bo, HttpServletResponse response) {
        List<AppUserVo> list = appUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户管理", AppUserVo.class, response);
    }

    /**
     * 获取用户管理详细信息
     *
     */
    @GetMapping("/userInfo")
    public R<AppUserVo> getInfo() {
        LoginAppUser loginUser = LoginHelper.getLoginAppUser();
        Long userId=loginUser.getUserId();
        return R.ok(appUserService.queryById(userId));
    }

    /**
     * 新增用户管理
     */
    @SaCheckPermission("app:user:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppUserBo bo) {
        appUserService.insertByBo(bo);
        return R.ok();
    }

    /**
     * 修改用户管理
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppUserBo bo) {
        LoginAppUser loginUser = LoginHelper.getLoginAppUser();
        Long userId=loginUser.getUserId();
        bo.setUserId(userId);
        appUserService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除用户管理
     *
     * @param userIds 主键串
     */
    @SaCheckPermission("app:user:remove")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userIds) {
        appUserService.deleteByIds(List.of(userIds));
        return R.ok();
    }
}
